//
//  PersonalHomeViewController.m
//  MiaoTu
//
//  Created by dllo on 16/9/1.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PersonalHomeViewController.h"
#import "UIView+Frame.h"
#import "Masonry.h"
#import "PersonHeaderView.h"
#import "PictureTableCell.h"
#import "UserInfo.h"
#import "Items.h"
#import "ZWYNetTool.h"
#import "Login.h"
#import "MineModel.h"
#import "MineTableViewCell.h"
#import "PersonalDataTableCell.h"
#import "PersonData.h"


#define kMineTableViewCell @"MineTableViewCellIdentifier"
#define kPictureTableCell @"PictureTableCellIdentifier"
#define kPersonalDataTableCell @"PersonalDataTableCellIdentifier"


@interface PersonalHomeViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray<Photo *> *picArray;
@property (nonatomic, strong) PersonHeaderView *headerView;
@property (nonatomic, strong) Login *login;
@property (nonatomic, strong) NSMutableArray<MineModel *> *myMineModelArray;
@property (nonatomic, strong) NSMutableArray<PersonData *> *personDataArray;

@end

@implementation PersonalHomeViewController

- (instancetype)init {
    if (self = [super init]) {
        _isOtherInfo = YES;
    }
    return self;
}

- (void)setUid:(NSString *)Uid {
    _Uid = Uid;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    if (_isOtherInfo) {
        NSLog(@"别人的主页");
    } else {
        NSLog(@"我的主页");
    }

    [self createTableView];
    [self createData];
    
}

- (void)createData {
//    _Uid = @"abf7763c-6a90-11e6-8ccf-00163e002e59";
    
    NSString *urlString = [NSString stringWithFormat:@"http://api.miaotu.net/v2/user?token=abf776af-6a90-11e6-8ccf-00163e002e59&uid=%@", _Uid];
    NSDictionary *headerDic = @{@"User-Agent":@"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C) Paros/3.2.13", @"Host":@"api.miaotu.net", @"Connection":@"Keep-Alive"};
    [ZWYNetTool GET:urlString andBody:nil andHeader:headerDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.login = [Login modelWithDic:result];
        self.picArray = _login.Items.PhotoList;
        _headerView.items = _login.Items;       
        // 个人资料Data
        [self createPersonalData];
        
        [_tableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
    MineModel *myFaqiYue = [MineModel mineWithTitle:@"我发起的约游" picName:@"myFaqiYue"];
    MineModel *myBaomingYue = [MineModel mineWithTitle:@"我报名的约游" picName:@"myBaomingYue"];
    MineModel *myLikeYue = [MineModel mineWithTitle:@"我喜欢的约游" picName:@"myLikeYue"];
    self.myMineModelArray = @[myFaqiYue, myBaomingYue, myLikeYue].mutableCopy;
    
    
}

- (void)createPersonalData {
    self.personDataArray = [NSMutableArray array];
    // 需要判断哪些值:Home, Work, GraduateSchool, LifeArea, WorkArea, FreeTime, Tags
    if (self.login.Items.Home.length > 0) {
        PersonData *Home = [PersonData personDataWithTitle:@"家乡" value:self.login.Items.Home];
        [_personDataArray addObject:Home];
    }
    if (self.login.Items.Work.length > 0) {
        PersonData *Work = [PersonData personDataWithTitle:@"职业" value:self.login.Items.Work];
        [_personDataArray addObject:Work];
    }
    if (self.login.Items.GraduateSchool.length > 0) {
        PersonData *GraduateSchool = [PersonData personDataWithTitle:@"学校" value:self.login.Items.GraduateSchool];
        [_personDataArray addObject:GraduateSchool];
    }
    if (self.login.Items.LifeArea.length > 0) {
        PersonData *LifeArea = [PersonData personDataWithTitle:@"生活在" value:self.login.Items.LifeArea];
        [_personDataArray addObject:LifeArea];
    }
    if (self.login.Items.WorkArea.length > 0) {
        PersonData *WorkArea = [PersonData personDataWithTitle:@"工作在" value:self.login.Items.WorkArea];
        [_personDataArray addObject:WorkArea];
    }
    if (self.login.Items.FreeTime.length > 0) {
        PersonData *FreeTime = [PersonData personDataWithTitle:@"有闲" value:self.login.Items.FreeTime];
        [_personDataArray addObject:FreeTime];
    }
    if (self.login.Items.Tags.length > 0) {
        PersonData *Tags = [PersonData personDataWithTitle:@"个人标签" value:self.login.Items.Tags];
        [_personDataArray addObject:Tags];
    }
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.dk_width, self.view.dk_height - 49 - 64) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableHeaderView = [self createTableHeaderView];
    [_tableView registerClass:[PictureTableCell class] forCellReuseIdentifier:kPictureTableCell];
    [_tableView registerNib:[UINib nibWithNibName:@"MineTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kMineTableViewCell];
    [_tableView registerNib:[UINib nibWithNibName:@"PersonalDataTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kPersonalDataTableCell];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (UIView *)createTableHeaderView {
    self.headerView = [PersonHeaderView headerView];
    _headerView.frame = CGRectMake(0, 0, _tableView.dk_width, 317);
    return _headerView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_isOtherInfo) {
        return 2;
    }
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        if (_isOtherInfo) {
            return 1;
        }
        return 3;
    } else if (section == 2) {
        return _personDataArray.count;
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 150;
    } else {
        return 45;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        PictureTableCell *cell = [tableView dequeueReusableCellWithIdentifier: kPictureTableCell forIndexPath:indexPath];
        cell.picArray = _picArray;
        return cell;
    } else if (indexPath.section == 1) {
        if (_isOtherInfo) {
            MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMineTableViewCell forIndexPath:indexPath];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.mineModel = [[MineModel alloc] initWithTitle:@"TA发起的约游" picName:@"myYueyou"];
            return cell;
        }
        MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMineTableViewCell forIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.mineModel = _myMineModelArray[indexPath.row];
        return cell;
        
    } else if (indexPath.section == 2) {
        PersonalDataTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kPersonalDataTableCell forIndexPath:indexPath];
        cell.personData = _personDataArray[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.dk_width, 50)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 50)];
    label.textColor = [UIColor colorWithRed:0.627 green:0.627 blue:0.647 alpha:1.000];
    if (section == 0) {
        label.text = @"照片墙";
    } else if (section == 1){
        if (_isOtherInfo) {
            label.text = @"TA的约游";
        } else {
            label.text = @"我的约游";
        }
    } else {
        label.text = @"个人资料";
    }
    [view addSubview:label];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
